package com.sinosoft.ci.patch;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.sinosoft.ci.utils.FileSystemUtils;

/**
 * 电商程序入口
 * @author 韩朝祥
 *
 */
public class EProgram {
	private File appRootDir;
	private String[] javaList;
	private String[] uiList;
	/** 补丁文件名 */
	private String patchFileName;
	

	public static void main(String[] args) {
		if(null == args || args.length < 1){
			usage();
			return;
		}

		String cmd = args[0];

		if("extract".equals(cmd)){
			if(args.length < 2){
				throw new RuntimeException("请指定清单 Excel!");
			}
			File excel = new File(args[1]);
			EProgram p = new EProgram();
			p.parseExcel(excel);
			p.extract();
			return;
		}else if("getTemplate".equals(cmd)){
			EProgram p = new EProgram();
			p.getTemplate();
		}else{
			usage();
		}

	}
	
	public void extract(){
		JavaEEWebIncrementEntryGenerator g1 = new JavaEEWebIncrementEntryGenerator(appRootDir);
		
		for(String j : javaList){
			if(j.toLowerCase().endsWith(".java")){
				g1.addJavaEntry(j);
			}else{
				g1.addClassPathEntry(j);
			}
		}
		
		for(String u : uiList){
			g1.addUIEntry(u);
		}
		
		String[] patchEtrys = g1.getPatchEntrys();
		
		File curr = FileSystemUtils.getWorkDir();
		IncrementPatchGenerator g2 = new IncrementPatchGenerator(appRootDir, curr);
		g2.extractPatch(patchEtrys, patchFileName);
		
		System.out.println("处理完成！");
	}

	public void parseExcel(File excel){
		try{
			Workbook wb = WorkbookFactory.create(excel);
			
			Sheet configSheet = wb.getSheet("config");
			Cell appRootConfigCell = configSheet.getRow(0).getCell(1);
			appRootDir = new File(appRootConfigCell.getStringCellValue());

			Cell patchFileNameCell = configSheet.getRow(1).getCell(1);
			patchFileName = patchFileNameCell == null || patchFileNameCell.getStringCellValue().trim().isEmpty()?
					"patch.zip" : patchFileNameCell.getStringCellValue();
			
			Sheet javaSheet = wb.getSheet("Java");
			
			List<String> javaList = new LinkedList<String>();
			Row row = null;
			
			int rowIndex = 0;
			while((row = javaSheet.getRow(rowIndex)) != null){
				Cell cell = row.getCell(0);
				if(null != cell){
					String e = cell.getStringCellValue();
					if(e != null && !e.trim().isEmpty()){
						javaList.add(e);
					}
				}
				rowIndex++;
			}
			
			this.javaList = javaList.toArray(new String[javaList.size()]);
			
			Sheet uiSheet = wb.getSheet("UI");
			List<String> uiList = new LinkedList<String>();
			row = null;
			rowIndex = 0;
			while((row = uiSheet.getRow(rowIndex)) != null){
				Cell cell = row.getCell(0);
				if(null != cell){
					String e = cell.getStringCellValue();
					if(e != null && !e.trim().isEmpty()){
						uiList.add(e);
					}
				}
				rowIndex++;
			}
			this.uiList = uiList.toArray(new String[uiList.size()]);
			
		}catch(Exception ex){
			throw new RuntimeException("清单文件解析失败！",ex);
		}
	}

	/**
	 * 获取模板
	 */
	private void getTemplate(){
		InputStream in = EProgram.class.getResourceAsStream("file-list.xlsx");
		if(null == in){
			System.err.println("加载模板失败！");
		}
		FileOutputStream out = null;
		try{
			File file = new File("file-list.xlsx");
			out = new FileOutputStream(file);
			IOUtils.copy(in,out);
		}catch(IOException ex){
			System.err.println("加载模板失败！");
		}finally {
			IOUtils.closeQuietly(in);
			IOUtils.closeQuietly(out);
		}
	}

	private static void usage(){

	}
}
